package com.example.practice.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.bean.ResultBean;
import com.example.practice.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class loginActivity_yl extends AppCompatActivity {
    private TextView tv_title_yl;
    private TextView tv_back_yl, tv_register, tv_find_pwd;
    private Button btn_login;
    private String userName,pwd, spPwd;
    private EditText et_userName, et_pwd;

    //标题布局
    private RelativeLayout rl_title_bar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_yl);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        initView();
    }

    private void initView() {
        rl_title_bar=findViewById(R.id.rl_bar);
        tv_title_yl=findViewById(R.id.tv_title);
        tv_title_yl.setText("登录");
        tv_back_yl=findViewById(R.id.tv_back);
        tv_register=findViewById(R.id.tv_register_yl);
        tv_find_pwd=findViewById(R.id.tv_find_pwd_yl);
        btn_login=findViewById(R.id.btn_login_yl);
        et_userName=findViewById(R.id.et_userName_yl);
        et_pwd=findViewById(R.id.et_password_yl);

        tv_back_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginActivity_yl.this.finish();
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(loginActivity_yl.this, registerActivity_yl.class);
                startActivityForResult(intent,1);
            }
        });

        tv_find_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(loginActivity_yl.this,findPasswordActivity.class);
                startActivityForResult(intent,1);
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_click();
            }
        });



    }

    /**
     * 获取注册页面返回的用户名
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            String userName=data.getStringExtra("userName");
            if(!TextUtils.isEmpty(userName)){
                et_userName.setText(userName);
                et_userName.setSelection(userName.length());
            }
        }
    }

    /**
     * 获取输入的用户名和密码
     */
    private void getEditString() {
        userName=et_userName.getText().toString().trim();
        pwd=et_pwd.getText().toString().trim();
    }

    /**
     * 从SP中根据用户名读取密码
     * @param userName
     * @return
     */
    private String readPwd(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(userName,"");

    }

    private void saveLoginStatus(boolean status,String userName){
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", status);
        editor.putString("loginUserName",userName);
        editor.commit();
    }

    private void btn_click(){
        getEditString();
        spPwd=readPwd(userName);
        if(TextUtils.isEmpty(userName)){
            Toast.makeText(loginActivity_yl.this,"请输入用户名",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(TextUtils.isEmpty(pwd)){
            Toast.makeText(loginActivity_yl.this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        else if((spPwd!=null&&!TextUtils.isEmpty(spPwd)&&!spPwd.equals(pwd))){
            Toast.makeText(loginActivity_yl.this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
            return;
        }

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userName",userName);
        jsonObject.addProperty("pwd", pwd);
        jsonObject.addProperty("kfz", "6");
        OkHttpUtils.sendOkHttp(false, true, "AndroidUser/login", jsonObject, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("res", "ff");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Log.i("res", res);
                Gson gson = new Gson();
                final ResultBean<String> result =  gson.fromJson(res, ResultBean.class);

                loginActivity_yl.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(result.code==200) {
                            Toast.makeText(loginActivity_yl.this,"登陆成功",Toast.LENGTH_SHORT).show();
                            saveLoginStatus(true,userName);
                            Intent data=new Intent();
                            data.putExtra("isLogin",true);
                            setResult(RESULT_OK,data);
                            loginActivity_yl.this.finish();
                        } else {
                            Toast.makeText(loginActivity_yl.this,"用户名不存在",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}